package com.case6.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.case5.pojo.BaseDataPojo;
import com.case5.service.LoginService;
import com.case5.util.HttpUtil;
import com.google.gson.Gson;
import com.utils.QiniuUtil;
import com.utils.TLSSigAPIv2;

/**
 * Servlet implementation class Login
 */
@WebServlet("/getUserId")
public class GetUserSig extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public GetUserSig() {
        super();
    }

    /**
	 * 腾讯云 SDKAppId，需要替换为您自己账号下的 SDKAppId。
	 * 进入腾讯云云通信[控制台](https://console.cloud.tencent.com/avc ) 创建应用，即可看到 SDKAppId，
	 * 它是腾讯云用于区分客户的唯一标识。
	 */
    
    public static final long SDKAppId = 1400528801;
    /**
	 * 计算签名用的加密密钥，获取步骤如下： step1.
	 * 进入腾讯云云通信[控制台](https://console.cloud.tencent.com/avc ) ，如果还没有应用就创建一个，
	 * step2. 单击“应用配置”进入基础配置页面，并进一步找到“帐号体系集成”部分。 step3. 点击“查看密钥”按钮，就可以看到计算
	 * UserSig 使用的加密的密钥了。
	 */
    
    public static final String SECRETKEY = "a91ce1e43c2048a288caa017dc35b47d936383ba5ed359d1b305f70632216abb";
    
    /**
   	 * 签名过期时间，建议不要设置的过短 时间单位：秒 默认时间：7 x 24 x 60 x 60 = 604800 = 7 天
   	 */
    public static final int EXPIRETIME = 604800;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 设置前后端交互参数的编码为utf-8		
				req.setCharacterEncoding("utf-8");
				resp.setContentType("text/html;charset=utf-8");
				// 用PrintWriter对象返回数据
				PrintWriter out = resp.getWriter();
				
				// 获取小程序端传过来的参数，key:用于图片命名的参数
				String userId = req.getParameter("userId");
				System.out.println("userId="+userId);
				
				// 生成用户的签名
				TLSSigAPIv2 tlsSigAPIv2 = new TLSSigAPIv2(SDKAppId,SECRETKEY);
				
				String userSig =  tlsSigAPIv2.genSig(userId, EXPIRETIME);
				System.out.println("userSig:"+userSig);
				
				
				Map<String, String> map = new HashedMap<>();
				map.put("userSig", userSig);
				if(userSig != null){
					out.print(new Gson().toJson(new 
							BaseDataPojo<Map<String, String>>
					("获取userSig成功", true, map)));
				}else{
					out.print(new Gson().toJson(new 
							BaseDataPojo<Map<String, String>>
					("获取userSig失败", false, null)));
				}
				
				
				
				// 获取上传凭证
//				token = QiniuUtil.getToken(key);
//				System.out.println("token="+token);
//				Map<String,String> map = new HashedMap<>();
//				map.put("token", token);
//				if(token!=null) {
//					out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
//							String>>("获取成功", true, map)));
//				}else {
//					out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
//							String>>("获取失败", false, map)));
//				}
				
		}
	
}
